//工具库

const crypto = require("crypto");

const jsonwebtoken = require("jsonwebtoken");

const fs = require('fs');

const path = require('path');

const uuid = require('uuid');

const nodemailer = require("nodemailer");

let transporter = nodemailer.createTransport({
  host: CONFIG.EMAIL_OPTIONS.HOST,
  port: CONFIG.EMAIL_OPTIONS.PORT,
  secure: CONFIG.EMAIL_OPTIONS.SECURE, // true for 465, false for other ports
  auth: {
    user: CONFIG.EMAIL_OPTIONS.USER,
    pass: CONFIG.EMAIL_OPTIONS.PASS,
  },
});

class Utils {
  constructor() {
    this.codes = [
      "A",
      "B",
      "C",
      "D",
      "E",
      "F",
      "G",
      "H",
      "I",
      "J",
      "K",
      "L",
      "M",
      "N",
      "O",
      "P",
      "Q",
      "R",
      "S",
      "T",
      "U",
      "V",
      "W",
      "X",
      "Y",
      "Z",
      "a",
      "b",
      "c",
      "d",
      "e",
      "f",
      "g",
      "h",
      "i",
      "j",
      "k",
      "l",
      "m",
      "n",
      "o",
      "p",
      "q",
      "r",
      "s",
      "t",
      "u",
      "v",
      "w",
      "x",
      "y",
      "z",
      0,
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
    ];
  }

  //创建6位验证码
  createSixValidCode() {
    let validCode = [];

    for (let i = 0; i < 6; i++) {
      let code = this.codes[Math.floor(Math.random() * this.codes.length)];
      validCode.push(code);
    }

    return validCode.join("");
  }

  //发送邮件
  sendEmail(options) {
    return new Promise((resolve, reject) => {
      transporter.sendMail(
        {
          from: CONFIG.EMAIL_OPTIONS.USER,
          to: options.emails,
          subject: options.subject,
          text: options.text,
        },
        (err, info) => {
          if (err) {
            reject(err);
          } else {
            resolve(info);
          }
        }
      );
    });
  }

  //加密字符串
  encodeString(value) {
    //使用md5方式加密value, 并且以十六进制字符串输出(32位字符串)
    return crypto.createHash("md5").update(value).digest("hex");
  }

  //创建token
  signToken(value) {
    return jsonwebtoken.sign(
      {
        data: value,
      },
      CONFIG.TOKEN_OPTIONS.SALT,
      {
        expiresIn: CONFIG.TOKEN_OPTIONS.EXPIRES,
      }
    );
  }

  //解析token
  verifyToken(token) {
    return new Promise((resolve, reject) => {
      jsonwebtoken.verify(token, CONFIG.TOKEN_OPTIONS.SALT, (err, decoded) => {
        if (err) {
          reject(err);
        } else {
          resolve(decoded);
        }
      });
    });
  }

  
  //上传图片
  uploadImg(options) {

    return new Promise((resolve, reject) => {
      //将base64转二进制
      let fileData = Buffer.from(options.base64, 'base64');
      // 

      //文件名
      let filename = uuid.v1() + '.' + options.type;

      let fileUrl = path.resolve(__basename, `upload/${filename}`);
      // 

      fs.writeFile(fileUrl, fileData, err => {
        //如果写入失败
        if (err) {
          reject(err);
        } else {
          resolve(CONFIG.UPLOAD_URL.URL + '/' + filename);
        }
      })
    })


  }
}

module.exports = new Utils();
